package com.wechet.utils;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import com.wechet.domain.OrderDetail;
import com.wechet.dto.OrderDto;
import com.wechet.exception.SellException;
import com.wechet.form.OrderForm;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sc on 2017/9/23.
 */
public class OrderForm2OrderdDTO {

    public static OrderDto convert(OrderForm orderForm){
        Gson gson = new Gson();
        OrderDto orderDto  = new OrderDto();
        orderDto.setBuyerName(orderForm.getName());
        orderDto.setBuyerPhone(orderForm.getPhone());
        orderDto.setBuyerAddress(orderForm.getAddress());
        orderDto.setBuyerOpenid(orderForm.getOpenid());
        List<OrderDetail> orl = new ArrayList<>();
        try {
            orl  = gson.fromJson(orderForm.getItems(),
                    new TypeToken<List<OrderDetail>>(){}.getType());
        } catch (JsonSyntaxException e) {
            throw  new SellException("参数不正确");
        }
        orderDto.setOrderDetailList(orl);
        return orderDto;
    }
}
